// gfreebox - A Freebox remote control
//
// Copyright (C) 2011 Laurent BRULET
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2 of the License, or (at your option)
// any later version.
// 
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
#include "ConfigurationManager.h"

#include <string>
#include <gconfmm.h>

static const Glib::ustring dir("/apps/gfreebox");

void ConfigurationManager::SetCode(std::string code)
{
    Glib::ustring key = dir + "/code";
    Glib::ustring schemaKey = "/schemas" + key;

    Glib::RefPtr<Gnome::Conf::Client> client = Gnome::Conf::Client::get_default_client();
    if (! client->dir_exists(dir))
    {
        printf("Toto\n");
        client->add_dir(dir, Gnome::Conf::CLIENT_PRELOAD_RECURSIVE);
    }
    client->set(key, code);
    client->notify(key);
}

std::string ConfigurationManager::GetCode()
{
    Glib::RefPtr<Gnome::Conf::Client> client = Gnome::Conf::Client::get_default_client();
    Gnome::Conf::Value v = client->get(dir + "/code");
    if (v.get_type() == Gnome::Conf::VALUE_STRING)
    {
        return v.get_string();
    }
    else
    {
        return std::string("");
    }
}

void ConfigurationManager::ClearConfiguration()
{
    printf("ClearConfiguration\n");
    Glib::RefPtr<Gnome::Conf::Client> client = Gnome::Conf::Client::get_default_client();
    client->recursive_unset(dir);
    client->notify(dir);
}

